package com.datang.petstore.aop;


import com.datang.petstore.entity.UserInfo;
import com.datang.petstore.exception.AcceptResultCodeException;
import com.datang.petstore.model.BaseUserModel;
import com.datang.petstore.resultpage.ResultCode;
import com.datang.petstore.service.UserService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-10-30 15:56
 * @notify 对用户进行验证,判断是否是被禁止登录的
 * @version 1.0
 */
@Aspect
@Component
public class ForbidAspect {

    @Autowired
    private UserService userService;

    @Pointcut("@annotation(com.datang.petstore.aop.ForbidAnnotation)")
    public void Pointcut() {
    }

    @Before("Pointcut()")
    public void userForbid(JoinPoint point) {
        //取出参数
        Object[] args = point.getArgs();
        BaseUserModel baseUserModel = (BaseUserModel) args[0];
        //拿到id
        String userId = baseUserModel.getUserId();
        //判断id是否被禁止登录
        UserInfo userInfo = userService.getUserInfo(userId);

        if (userInfo.getForbid().equals("1")) {
            throw new AcceptResultCodeException(ResultCode.error503);
        }
    }
}
